package com.tuandai.commons.hbase;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * Result utils
 * Created by chenpi on 2017/3/20.
 */
public class ResultUtils {

    /**
     * 获取字段值
     *
     * @param result
     * @param qualifier
     * @return
     */
    public static String getValue(Result result, String qualifier) {
        byte[] valBytes = result.getValue(Bytes.toBytes(Constants.FAMILY_DEFAULT), Bytes.toBytes(qualifier));
        if (valBytes != null) {
            String value = Bytes.toString(valBytes);
            if (value.equals("null")) {
                return null;
            }
            return value;
        }
        return null;
    }

    /**
     * 获取double 类型值
     *
     * @param result
     * @param qualifier
     * @return
     */
    public static Double getDoubleValue(Result result, String qualifier) {
        String valueStr = getValue(result, qualifier);
        if (valueStr == null)
            return null;
        try {
            return Double.parseDouble(valueStr);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 获取整数类型值
     *
     * @param result
     * @param qualifier
     * @return
     */
    public static Integer getIntegerValue(Result result, String qualifier) {
        String valueStr = getValue(result, qualifier);
        if (valueStr == null)
            return null;
        try {
            return Integer.parseInt(valueStr);
        } catch (NumberFormatException e) {
            return null;
        }
    }

    /**
     * 获取长整形数据
     * @param result result
     * @param qualifier qualifier
     * @return
     */
    public static Long getLongValue(Result result,String qualifier){
        String valueStr = getValue(result, qualifier);
        if (valueStr == null)
            return null;
        try {
            return Long.valueOf(valueStr);
        } catch (NumberFormatException e) {
            return null;
        }
    }

}
